package com.zhonghui.modlules.product.eshopService.Impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.modlules.cart.eshopVo.StoreCartQueryVo;
import com.zhonghui.modlules.product.eshopDomain.StoreProductReply;
import com.zhonghui.modlules.product.eshopMapper.StoreProductReplyMapper;
import com.zhonghui.modlules.product.eshopService.StoreProductReplyService;
import com.zhonghui.modlules.product.eshopVo.ReplyCountVo;
import com.zhonghui.modlules.product.eshopVo.StoreProductReplyQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true,
        rollbackFor = {Exception.class}
)
public class StoreProductReplyServiceImpl extends BaseServiceImpl<StoreProductReplyMapper, StoreProductReply> implements StoreProductReplyService {
    @Override
    public int replyCount(String unique) {
        LambdaQueryWrapper<StoreProductReply> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreProductReply::getUnique, unique);
        return ((StoreProductReplyMapper)this.baseMapper).selectCount(wrapper);
    }

    public ReplyCountVo getReplyCount(long productId) {
        int sumCount = this.productReplyCount(productId);
        if (sumCount == 0) {
            return new ReplyCountVo();
        } else {
            LambdaQueryWrapper<StoreProductReply> queryWrapper = new LambdaQueryWrapper<>();

            LambdaQueryWrapper<StoreProductReply> eq = queryWrapper.eq( StoreProductReply::getProductId, productId ).eq( StoreProductReply::getProductScore, 5 );
            int goodCount = ((StoreProductReplyMapper)this.baseMapper).selectCount(eq);

            LambdaQueryWrapper<StoreProductReply> gt = queryWrapper.eq( StoreProductReply::getProductId, productId ).lt( StoreProductReply::getProductScore, 5 ).gt( StoreProductReply::getProductScore, 2 );
            int inCount = ((StoreProductReplyMapper)this.baseMapper).selectCount(gt);

            LambdaQueryWrapper<StoreProductReply> le = queryWrapper.eq( StoreProductReply::getProductId, productId ).le( StoreProductReply::getProductScore, 2 );
            int poorCount = ((StoreProductReplyMapper)this.baseMapper).selectCount(le);

            String replyChance = "" + NumberUtil.round(NumberUtil.mul(NumberUtil.div((float)goodCount, (float)sumCount), 100.0F), 2);
            String replyStar = "" + NumberUtil.round(NumberUtil.mul(NumberUtil.div((float)goodCount, (float)sumCount), 5.0F), 2);
            return ReplyCountVo.builder().sumCount(sumCount).goodCount(goodCount).inCount(inCount).poorCount(poorCount).replyChance(replyChance).replySstar(replyStar).build();
        }
    }

    public int productReplyCount(long productId) {
        LambdaQueryWrapper<StoreProductReply> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<StoreProductReply> eq = queryWrapper.eq( StoreProductReply::getProductId, productId );
        return ((StoreProductReplyMapper)this.baseMapper).selectCount(eq);
    }

    public List<StoreProductReplyQueryVo> getReplyList(long productId, int type, int page, int limit) {
        List<StoreProductReplyQueryVo> newList = new ArrayList();
        Page<StoreProductReply> pageModel = new Page((long)page, (long)limit);
        List<StoreProductReplyQueryVo> list = ((StoreProductReplyMapper)this.baseMapper).selectReplyList(pageModel, productId, type);
        List<StoreProductReplyQueryVo> list1 = (List)list.stream().map((i) -> {
            StoreProductReplyQueryVo vo = new StoreProductReplyQueryVo();
            BeanUtils.copyProperties(i, vo);
            if (i.getPictures().contains(",")) {
                vo.setPics(i.getPictures().split(","));
            }

            return vo;
        }).collect( Collectors.toList());
        Iterator var10 = list1.iterator();

        while(var10.hasNext()) {
            StoreProductReplyQueryVo queryVo = (StoreProductReplyQueryVo)var10.next();
            newList.add(this.handleReply(queryVo));
        }

        return newList;
    }

    public StoreProductReplyQueryVo handleReply(StoreProductReplyQueryVo replyQueryVo) {
        StoreCartQueryVo cartInfo = (StoreCartQueryVo) JSONObject.parseObject(replyQueryVo.getCartInfo(), StoreCartQueryVo.class);
        if (ObjectUtil.isNotNull(cartInfo) && ObjectUtil.isNotNull(cartInfo.getProductInfo()) && ObjectUtil.isNotNull(cartInfo.getProductInfo())) {
            replyQueryVo.setSku( (String) cartInfo.getProductInfo() );
        }

        BigDecimal star = NumberUtil.add(replyQueryVo.getProductScore(), replyQueryVo.getServiceScore());
        star = NumberUtil.div(star, 2);
        replyQueryVo.setStar(String.valueOf(star.intValue()));
        if (StrUtil.isEmpty(replyQueryVo.getComment())) {
            replyQueryVo.setComment("此用户没有填写评价");
        }

        return replyQueryVo;
    }
}
